package com.nuaa.ange.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nuaa.ange.annotation.ApiLog;
import com.nuaa.ange.common.lang.RespBean;
import com.nuaa.ange.entity.User;
import com.nuaa.ange.shiro.AccountProfile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.springframework.web.bind.annotation.*;

/**
 * @author 玦离
 * @description login register exit
 * @date 2022/12/30 22:06
 */

@RestController
@Api(tags = "登录注册相关接口")
public class AuthController extends BaseController{

    @ApiOperation(value = "测试1")
    @GetMapping("/test01")
    @ApiLog
    public void test01()
    {
        System.out.println(getProfileId());
    }

    @ApiOperation(value = "测试2")
    @GetMapping("/test02")
    @ApiLog
    public void test02()
    {
    }

    @ApiOperation(value = "注册用户")
    @GetMapping("/regUser")
    @ApiLog
    public RespBean reg(@RequestParam("phoneNumber") String phoneNumber) {
        return userService.regUser(phoneNumber);
    }

    @ApiOperation(value = "注册用户信息")
    @PostMapping("/regUserInfo")
    @ApiLog
    public RespBean regUser(@RequestBody User user) {
        return userService.regUserInfo(user);
    }

    @ApiOperation(value = "登录")
    @GetMapping("/login")
    @ApiLog
    public RespBean login(@RequestParam("phoneNumber") String phoneNumber, @RequestParam("password") String password) {
        if(StrUtil.isEmpty(phoneNumber) || StrUtil.isBlank(password)) {
            return RespBean.error("手机号或密码不能为空");
        }
        try {
            User user = userService.getOne(new QueryWrapper<User>()
                    .eq("phone_number", phoneNumber)
                    .eq("user_role", true));
            UsernamePasswordToken token = new UsernamePasswordToken(phoneNumber, SecureUtil.md5(password + user.getSalt()));
            SecurityUtils.getSubject().login(token);
            return RespBean.ok("登陆成功", user);
        } catch (AuthenticationException e) {
            if (e instanceof UnknownAccountException) {
                return RespBean.error("用户不存在");
            } else if (e instanceof LockedAccountException) {
                return RespBean.error("用户被禁用");
            } else if (e instanceof IncorrectCredentialsException) {
                return RespBean.error("密码错误");
            } else {
                return RespBean.error("用户认证失败");
            }
        }
    }

    @ApiOperation(value = "用户切换")
    @GetMapping("/switch")
    @ApiLog
    public RespBean switchId() {
        User switchUser = userService.getOne(new QueryWrapper<User>()
                .eq("phone_number", getProfile().getPhoneNumber())
                .eq("user_role", !getProfile().getUserRole()));
        AccountProfile profile = getProfile();
        profile.setUserId(switchUser.getUserId());
        profile.setUserRole(switchUser.getUserRole());
        return RespBean.ok("切换状态成功", switchUser);
    }

    @ApiOperation(value = "用户退出登录")
    @RequestMapping("/logout")
    @ApiLog
    public RespBean logout() {
        SecurityUtils.getSubject().logout();
        return RespBean.ok("退出登录成功");
    }

}
